$(document).ready(function() {
	$("#frmJoinBet").validate({
		rules: {
		},
		messages: {
		},
		submitHandler : function(form) {
			var betPosId = $('#frmJoinBet_joined_betPosId').val();
			var betId = $('#frmJoinBet_joined_betId').val();
			var URL = BET_VIEWBET_URL + "?betInfo.betId=" + betId;
			$("#frmJoinBet_label_stake_" + betPosId).addClass("hide");
			var stake    = $("#frmJoinBet_stake_" + betPosId).val();
			if (stake == '') {
				alert(message_bet_joinbet_stake_required);
				return false;
			}
			var data = {
				"custBetPosInfo.betPosId"		: betPosId,
				"custBetPosInfo.stake"			: stake				
			};
			$.ajax({
				type: 'POST',
				url : BET_JOIN_BET_URL,
				data: data,
				success : function(data) {
					if (data.errorMessage != '') {
						alert(data.errorMessage);
					} else {
						if (data.successMessage != '') {
							alert(data.successMessage);
							window.location.href = URL;
						}
					}
				}
			});
		}
	});
});

function joinBet(betPosId, betId) {
	$('[id^="frmJoinBet_stake_"]').each(function() {
		$(this).rules("remove");
	});
	$('#frmJoinBet_stake_' + betPosId).rules("add", {
		required : true,
		maxlength : 15,
		number: true,
		min: 1.1,
		maxTo: '#frmJoinBet_join_betPosId' + betPosId + '_betableamount',
		messages : {
			required : message_bet_joinbet_stake_required,
			maxlength : message_bet_joinbet_stake_maxlength,
			number: message_bet_joinbet_stake_number,
			min: message_bet_joinbet_stake_min,
			maxTo: message_bet_joinbet_stake_maxto
		}
	});
	$('#frmJoinBet_joined_betPosId').val(betPosId);
	$('#frmJoinBet').submit();
}